<?php


namespace app\api\controller;

use app\api\model\Device as DeviceModel;
use think\Db;
use app\api\model\DeviceOrder as DeviceOrderModel;
use app\api\model\User as UserModel;

class Device extends Base
{
    /***
     * Desc:操作设备
     * User: HAIYUN
     * Date: 2022/3/2 11:05
     */
    public function operateDevice() {
        //获取用户
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new UserModel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //校验参数
        $rules = [
            ['device_number','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        //查询设备信息
        $devcie = new DeviceModel();
        $where  = [
            'status' => '1',//以启用的设备
            'device_number' => $this->params['device_number'],
        ];
        $device_info = $devcie->where($where)->find();
        if(!$device_info){
            $this->return_msg('20004','未查询到该设备信息',[]);
        }
        $device_order = new DeviceOrderModel();
        $msg = '租用书柜成功'; //提示信息
        Db::startTrans();//开启事务
        try {
            //根据设备是否绑定用户id判断操作(以绑定则为购买,未绑定则为寄售)
            if(!$device_info['user_id']){
                //寄售书籍操作
                //1.添加设备订单
                $order_data = [
                    'device_uuid' => $this->get_order_uuid(),
                    'status' => 1,
                    'device_number' => $this->params['device_number'],
                    'start_user_id' => $uid,
                ];
                $device_order->add($order_data);
                //2.设备绑定用户
                $devcie->edit(['user_id'=>$uid],['device_number'=>$this->params['device_number']]);
            }else{
                //修改订单状态为待支付
                $datetime = new \DateTime;
                $end_time = $datetime->format('Y-m-d H:i:s');
                //查询订单信息,获取订单开始时间
                $where = [
                    'device_number' => $this->params['device_number'],
                    'status' => 1,
                ];
                $order_info = $device_order->where($where)->find();
                //计算订单时长,收费
//                $payment = $this->diffBetweenTwoDays($order_info['create_time'],$end_time);
                if($uid == $device_info['user_id']){ //自己拿出自己的商品,只计算时间收费,不购买
                    $payment = $this->diffBetweenTwoDays($order_info['create_time'],$end_time);
                    $msg = "租用书柜结束";
                }else{//别人打开柜子,为购买商品
                    $payment = $device_info['price'];
                    $msg = "购买商品成功";
                }
                $device_order->edit(['status'=>2,'end_user_id'=>$uid,'end_time'=>$end_time,'payment'=>$payment],['device_uuid'=>$order_info['device_uuid']]);
                //扣款
                //....
                //解绑设备
                $devcie->edit(['user_id'=>null,'price'=>0],['device_number'=>$this->params['device_number']]);
                //增加用户账单记录
            }
            // 提交事务
            Db::commit();
            $this->return_msg('00000',$msg,[]);
        }catch (\Exception $e){
            //回滚事务
            Db::rollback();
            $this->return_msg('20006',$e->getMessage(),[]);
        }
    }

    /***
     * Desc:用户设置设备打开价格
     * User: HAIYUN
     * Date: 2022/3/2 15:49
     */
    public function setPrice() {
        //获取用户
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new UserModel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //校验参数
        $rules = [
            ['price','require','10001'],
            ['device_number','require','10001'],
        ];
        $msg   = $this->validate($this->params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        $safe_data = $this->getDataByRule($this->params, $rules);
        $devcie = new DeviceModel();
        $where  = [
            'status' => '1',//以启用的设备
            'device_number' => $safe_data['device_number'],
            'user_id' => $uid,
        ];
        $device_info = $devcie->where($where)->find();
        if(!$device_info){
            $this->return_msg('20004','未查询到该设备信息',[]);
        }
        //修改单价
        $res = $devcie->edit(['price'=>$safe_data['price']],$where);
        if($res){
            $this->return_msg('00000','修改书柜价格成功',$safe_data);
        }else{
            $this->return_msg('00000','修改书柜价格失败',[]);
        }
    }
}